package figure8.GUI;

import java.awt.GridLayout;
import java.awt.Toolkit;
import java.awt.event.ActionEvent;
import java.awt.event.ActionListener;
import java.io.File;
import java.io.FileOutputStream;
import java.io.ObjectOutputStream;

import javax.swing.BorderFactory;
import javax.swing.JButton;
import javax.swing.JDialog;
import javax.swing.JFileChooser;
import javax.swing.JLabel;
import javax.swing.JOptionPane;
import javax.swing.JPanel;
import javax.swing.JSpinner;
import javax.swing.JTextField;
import javax.swing.SpinnerNumberModel;
import javax.swing.border.EtchedBorder;

import figure8.Controller;
import figure8.DrumMachineTrack;
import figure8.TapTimer;
import figure8.LoopTiming;
import figure8.Project;

public class NewProjectWindow extends JDialog {
	private DrumMachineTrack metTest;
	private Controller controller;
	private DrumMachineTrack drumTrack;
	
	public NewProjectWindow(final Controller controller1, final MainWindow window, DrumMachineTrack drumTrack1) {
		super();
		controller = controller1;
		drumTrack = drumTrack1;
		this.setLocation(window.getFrame().getX() + 80, window.getFrame()
				.getY() + 120);
		this.setIconImage(Toolkit.getDefaultToolkit().getImage("src/figure8/GUI/Fig8-small.png"));
		this.setAlwaysOnTop(true);
		
		final JDialog projectWindow = this;	
		final JPanel panel = new JPanel(new GridLayout(6, 2, 2, 2));
		panel.setBorder(BorderFactory.createEtchedBorder(EtchedBorder.LOWERED));
		
		this.getContentPane().add(panel);
		
		panel.add(new JLabel("Project Name"));
		final JTextField name = new JTextField("Unnamed Project");
		panel.add(name);

		panel.add(new JLabel("Tempo"));
		final SpinnerNumberModel tempo = new SpinnerNumberModel(120, 1, 400,
				1);
		panel.add(new JSpinner(tempo));

		panel.add(new JPanel());
		final JButton test = new JButton("Test");
		
		final TapTimer tapTimer = new TapTimer();
		final JButton tap = new JButton("Tap Tempo");
		tap.addActionListener(new ActionListener() {
			public void actionPerformed(ActionEvent e) {
				tempo.setValue((int) tapTimer.Update());
			}
		});
		panel.add(tap);		
		panel.add(new JLabel("Bars Per Loop"));
		final SpinnerNumberModel bars = new SpinnerNumberModel(4, 1, 128, 1);
		panel.add(new JSpinner(bars));

		panel.add(new JLabel("Time Signature"));

		JPanel time = new JPanel(new GridLayout(1, 2));
		final SpinnerNumberModel note = new SpinnerNumberModel(4, 1, 32, 1);
		final SpinnerNumberModel beats = new SpinnerNumberModel(4, 1, 32, 1);
		time.add(new JSpinner(note));
		time.add(new JSpinner(beats));
		panel.add(time);
		if (drumTrack != null) metTest = drumTrack;
		else metTest = new DrumMachineTrack (new LoopTiming((int) tempo.getNumber().intValue(), (int) bars
						.getNumber().intValue(), note.getNumber().intValue(), (int) beats.getNumber().intValue()));
		test.addActionListener(new ActionListener() {
			public void actionPerformed(ActionEvent e) {
				LoopTiming timing = new LoopTiming((int) tempo.getNumber().intValue(), (int) bars
						.getNumber().intValue(), note.getNumber().intValue(), (int) beats.getNumber().intValue());
				metTest.switchTiming(timing);
				metTest.playInThread();
			}
		});
		panel.add(test);
		final JButton submit = new JButton("Create Project");
		submit.addActionListener(new ActionListener() {
			@Override
			public void actionPerformed(ActionEvent arg0) {
				LoopTiming timing = new LoopTiming((int) tempo.getNumber().intValue(), (int) bars
						.getNumber().intValue(), note.getNumber().intValue(), (int) beats.getNumber().intValue());
				//metTest.closeLines();
				NewProjectWindow.this.dispose();
				JFileChooser newFC = new JFileChooser();
				metTest.switchTiming(timing);
				newFC.setFileSelectionMode(JFileChooser.DIRECTORIES_ONLY);
				int returnVal = newFC.showDialog(window.getFrame(), "Create");
				if (returnVal == JFileChooser.APPROVE_OPTION) {
					String dir = newFC.getSelectedFile().getAbsolutePath();
					System.out.println("Filename = " + dir);
					File projectDir = new File(dir + File.separator + name.getText());
					File directory = new File(dir);
					if (isWorkingDirectory(directory)) {
						//controller.setProject(new Project(timing, name.getText(), projectDir));
						if(projectDir.exists())
						{
							ProjectAlreadyExistsWindow projExists = new ProjectAlreadyExistsWindow(
									controller, directory, name.getText(), timing);
						}
						else {
							controller.setProject(new Project(timing, name.getText(), directory), metTest);
						}
						
					}
					else {
						JOptionPane.showMessageDialog(controller.getWindow().getFrame(), "Error: Bad file choice. Please try again");
						NewProjectWindow newWindow = new NewProjectWindow(controller, controller.getWindow(), drumTrack);
					}

				} else {
					System.out.println("New project window cancel button clicked");
					metTest.closeLines();
				}
				projectWindow.dispose();
			}
		});

		panel.add(submit);

		this.pack();
		this.setVisible(true);
	}
	
	public boolean isWorkingDirectory (File dir) {
		File testFile = null;
		try
		{
			testFile = new File(dir.getAbsolutePath() + File.separator 
					+ "test.f8p");

			ObjectOutputStream stream = new ObjectOutputStream(new FileOutputStream(testFile));
			stream.close();
		}
		catch(Exception e)
		{	
			return false;
		}
		if (testFile != null) testFile.delete();
		return true;
	}
}
